<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午11:22
 */

class GameController extends AdminBaseController {

    public function index(){

        $grid   = new PubGrid();
		$filter = new PubFilter();

        $filter->addFilter('search', 'input', PubFunc::t('游戏名'));

        $params = $filter->get();

        // 数据展示
        $header = GameService::indexHeader();
        $data   = GameService::indexData($params);
        $grid->addGrid($header, $data);

        $this->out['grid']      = $grid->show();
        $this->out['filter']    = $filter->show();

        // 设置标题
        $this->setTitle('游戏列表', 'Platform Info');
    }

    public function add(){
        $form = new PubForm();

        $platformOptions = PlatformService::getOptions();

        $form->addItem('gamesrc', 'input', PubFunc::t('游戏代号'))
            ->addItem('gamename', 'input', PubFunc::t('游戏名称'))
            ->addItem('icon', 'input', PubFunc::t('游戏图标'))
            ->addItem('platform', 'checkbox', PubFunc::t('所属平台'), array('options' => $platformOptions))
            ->addItem('remark', 'textarea', PubFunc::t('简介'))
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
            ->addOperation('返回', 'a', PubFunc::buildUrl("/auth/?r=game/index"), 'btn btn-default')
            ->setAction(PubFunc::buildBackUrl("/auth/?r=game/doAdd"))
            ->setName('game');

        $this->out['form'] = $form->show();

        $this->setTitle('新增游戏', 'Add Platform');
    }

    public function doAdd(){
        $game = $this->htmlR('game');
        $result = GameService::add($game);
        if($result !== false){
            $this->showMessage(PubFunc::t('添加成功'), PubFunc::buildBackUrl("/auth/?r=game/index"));
        }else{
            $this->showMessage('添加失败');
        }
    }

    public function edit(){
        $form = new PubForm();

        $id = $this->R('id');
        $game = GameService::getGameById($id);

        $platformOptions = PlatformService::getOptions();

        $form->addItem('gamesrc', 'input', PubFunc::t('游戏代号'), array('value' => $game['GameSrc']))
            ->addItem('gamename', 'input', PubFunc::t('游戏名称'), array('value' => $game['GameName']))
            ->addItem('icon', 'input', PubFunc::t('游戏图标'), array('value' => $game['GameIcon']))
            ->addItem('platform', 'checkbox', PubFunc::t('所属平台'), array('options' => $platformOptions, 'value' => PlatformGameMapService::getPlatformByGame($id)))
            ->addItem('remark', 'textarea', PubFunc::t('简介'), array('value' => $game['Remark']))
            ->addValue('game[id]', $id)
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
//             ->addOperation('返回', 'a', '/auth/?r=game/index', 'btn btn-default')
//             ->setAction('/auth/?r=game/doEdit')
            ->addOperation('返回', 'a', PubFunc::buildBackUrl("/auth/?r=game/index"), 'btn btn-default')
            ->setAction(PubFunc::buildBackUrl("/auth/?r=game/doEdit"))
            ->setName('game');

        $this->out['form'] = $form->show();

        $this->setTitle('编辑游戏', 'Edit System');
    }

    public function doEdit(){
        $game = $this->htmlR('game');
        $result = GameService::edit($game);
        if($result !== false){
            $this->showMessage(PubFunc::t('修改成功'), PubFunc::buildBackUrl("/auth/?r=game/index"));
        }else{
            $this->showMessage(PubFunc::t('修改失败'));
        }
    }

} 